﻿angular.module('bookStoreServices', [])
    .factory('bookStoreData', function ($http) {

        //get sessionKey
        var sessionKey = localStorage.getItem("sessionKey");
        var headers = { 'X-sessionKey': sessionKey };

        //base url
        var baseURL = 'http://localhost:49733/api';

        return {
            //all users
            getUsers: function () {
                return $http(
                        {
                            method: "GET",
                            url: baseURL + "/users/all",
                            headers: headers
                        });
            },
            //user details
            getUserDetails: function (id) {
                return $http(
                        {
                            method: "GET",
                            url: baseURL + "/users/details?userId=" + id,
                            headers: headers
                        });
            },
            //change access level
            putUser: function (user) {
                return $http(
                        {
                            method: "PUT",
                            url: baseURL + "/users/access",
                            headers: headers,
                            data: user
                        })
            },
            //all stores
            getStores: function () {
                return $http(
                        {
                            method: "GET",
                            url: baseURL + "/stores",
                            headers: headers
                        });
            },
            //post new store
            postStore: function (store) {
                return $http(
                        {
                            method: "POST",
                            url: baseURL + "/stores",
                            headers: headers,
                            data: store
                        });
            },
            //get all books
            getBooks: function () {
                return $http(
                        {
                            method: "GET",
                            url: baseURL + "/books/all",
                            headers: headers
                        })
            },
            //post new book
            postBook: function (book) {
                return $http(
                        {
                            method: "POST",
                            url: baseURL + "/books",
                            headers: headers,
                            data: book
                        })
            }
        };
});